package com.example.collection;

import java.util.*;

/**
 * @description:
 * @author: qijiang
 * @date: 2019/8/12 15:35
 */

public class SimpleLinkedList<T> {

    private Node<T> pointer;// 当前指针节点
    private Node<T> firstNode;// 首个节点
    private Node<T> lastNode;// 末尾节点
    private Integer index = 0;// 当前指针index
    private Integer size = 0;// 当前容量




    /**
     *
     * @description: 默认向末尾添加
     * @author: qijiang @email: jiang1211@foxmail.com
     * @date: 2019/8/12 4:10 PM
     * @param: [t]
     * @return: void
     */
    public void add(T t){
        Node<T> node = new Node<>();
        //第一个
        if(0 == index){
            node.info=t;
            firstNode =node;
            pointer=node;
            lastNode=node;
        }else{
            T info = pointer.info;
            node.info=t;
            pointer.next= node;
            node.prve= pointer;
            pointer =node;
            lastNode=node;
        }
        index++;
    }

    /**
     *
     * @description: 选择位置添加
     * @author: qijiang @email: jiang1211@foxmail.com
     * @date: 2019/8/12 4:10 PM
     * @param: [t]
     * @return: void
     */
    public void add(T t,Integer paramIndex){
        isNullThrowRxception(t,paramIndex);
        isOutOffIndex(paramIndex);
        Node<T> node;
        //第一个位置添加
        if(0 == paramIndex){
            if(0 == index){
                add(t);
                return;
            }else{
                node = new Node<>();
                node.info=t;
                node.next=firstNode;

                firstNode.prve=node;
                firstNode=node;
                pointer=node;
            }
        }else if(index.equals(paramIndex)){
            //末尾添加
            add(t);
            return;
        }else{
            //中间添加
            node = new Node<>();
            node.info=t;
            Node<T> oldNode = getNodeByIndex(paramIndex);
            Node<T> prveOldNode = oldNode.prve;
            node.prve=oldNode.prve;
            node.next=oldNode;
            prveOldNode.next=node;
            oldNode.prve=node;
            pointer=node;
        }
        index++;

    }

    public T getByIndex(Integer paramIndex) {
        return getNodeByIndex(paramIndex).info;
    }

    /**
     *
     * @description: 获指定位置的数据
     * @author: qijiang @email: jiang1211@foxmail.com
     * @date: 2019/8/12 5:34 PM
     * @param: [paramIndex]
     * @return: com.example.collection.SimpleLinkedList<T>.Node<T>
     */
    public Node<T> getNodeByIndex(Integer paramIndex){
        isNullThrowRxception(paramIndex);
        isOutOffIndex(paramIndex);
        Node<T> node = firstNode;
        for (int i = 0; i < index; i++) {
            if(!paramIndex.equals(i)){
                node=node.next;
            }else{
                return node;
            }
        }
        return null;
    }
    /**
     * @description: 桶NODE
     * @author: qijiang
     * @date: 2019/8/12 15:35
     */
    private class Node<T>{

        private T info;
        private Node<T> prve;
        private Node<T> next;

        @Override
        public String toString() {
            return "Node [info=" + info + ", next=" + next + "]";
        }
    }

    public static void main(String[] args){
//        SimpleLinkedList<Integer> simpleLinkedList = new SimpleLinkedList();
//        LinkedList linkedList =new LinkedList();
//        simpleLinkedList.add(1);
//        simpleLinkedList.add(2);
//        simpleLinkedList.add(4);
//        simpleLinkedList.add(0,0);
//        simpleLinkedList.add(3,3);
//        linkedList.add(1);
//        linkedList.add(2);
//        linkedList.add(3);
//        System.out.println(simpleLinkedList);
//        System.out.println(simpleLinkedList.getNodeByIndex(0));
//        System.out.println(simpleLinkedList.getByIndex(1));
        User u = new User("qijiang","123456");
        List list = new ArrayList();
        AbstractList abstractList =new ArrayList();
        Map map = new HashMap();

        for (int i = 0; i < 10000; i++) {
            if(i ==0 || i==11 || i ==16 || i==23){
                System.out.println(1);
            }
            list.add(i);
            map.put(i,i);
        }
        u.setUserName("qqq");
        abstractList.add(u);
        u.setUserName("qqq");
        abstractList.add(u);

        System.out.println(list);
        System.out.println(abstractList);
    }

    /**
     *
     * @description: 参数为空时抛出异常
     * @author: qijiang @email: jiang1211@foxmail.com
     * @date: 2019/8/12 5:16 PM
     * @param: [param]
     * @return: void
     */
    private void isNullThrowRxception(Object... params){
        for (int i = 0; i < params.length; i++) {
            if(null == params[i]){
                throw new RuntimeException("param is not null");
            }
        }
    }
    /**
     *
     * @description: 超出坐标范围时抛出异常
     * @author: qijiang @email: jiang1211@foxmail.com
     * @date: 2019/8/12 5:16 PM
     * @param: [param]
     * @return: void
     */
    private void isOutOffIndex(Integer paramIndex){
        if(0>paramIndex || paramIndex > index){
            throw new RuntimeException("index out off");
        }
    }
}
